package com.example.myapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    private static final String TAG ="CustomAdapter" ;

    private Context context;
    Activity activity;
    private ArrayList my_id,my_age,my_height,my_weight,my_sex,my_type;

    CustomAdapter(Activity activity,Context context,
                  ArrayList my_id,
                  ArrayList my_age,
                  ArrayList my_height,
                  ArrayList my_weight,
                  ArrayList my_sex,
                  ArrayList my_type){
        this.context = context;
        this.activity = activity;
        this.my_id = my_id;
        this.my_age = my_age;
        this.my_height = my_height;
        this.my_weight = my_weight;
        this.my_sex = my_sex;
        this.my_type = my_type;


    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        //将列表的值和输入的值对应起来
           holder.my_id_txt.setText("第"+String.valueOf(my_id.get(position))+"次记录");
           holder.my_age_txt.setText("年龄："+String.valueOf(my_age.get(position)));
           holder.my_height_txt.setText("身高："+String.valueOf(my_height.get(position)));
           holder.my_weight_txt.setText("体重："+String.valueOf(my_weight.get(position)));
           holder.my_sex_txt.setText("性别："+String.valueOf(my_sex.get(position)));
           holder.my_type_txt.setText("运动类型："+String.valueOf(my_type.get(position)));

           //为列表的行添加事件触发，点击更新数据
           holder.mainLayout.setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   //点击每一栏就跳转到UpdateActivity
                   Intent intent = new Intent(context,UpdateActivity.class);
                   //并且将值放入Intent方便UpdateActivity里调用
                   intent.putExtra("id",String.valueOf(my_id.get(position)));
                   intent.putExtra("age",String.valueOf(my_age.get(position)));
                   intent.putExtra("height",String.valueOf(my_height.get(position)));
                   intent.putExtra("weight",String.valueOf(my_weight.get(position)));
                   intent.putExtra("sex",String.valueOf(my_sex.get(position)));
                   intent.putExtra("type",String.valueOf(my_type.get(position)));
                   activity.startActivityForResult(intent,1);
               }
           });

           //长按跳转界面
           holder.mainLayout.setOnLongClickListener(new View.OnLongClickListener() {
               @Override
               public boolean onLongClick(View v) {

                   Log.i(TAG,"onItemLongClick:"+position);
                   Intent intent2 = new Intent(context, CalculateActivity.class);//跳转到的页面，必备


                   intent2.putExtra("id1",String.valueOf(my_id.get(position)));
                   intent2.putExtra("age1",String.valueOf(my_age.get(position)));
                   intent2.putExtra("height1",String.valueOf(my_height.get(position)));
                   intent2.putExtra("weight1",String.valueOf(my_weight.get(position)));
                   intent2.putExtra("sex1",String.valueOf(my_sex.get(position)));
                   intent2.putExtra("type1",String.valueOf(my_type.get(position)));
                   activity.startActivityForResult(intent2,2);



                   return true;
               }
           });
    }

    @Override
    public int getItemCount() {
        return my_id.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView my_id_txt,my_age_txt,my_height_txt,my_weight_txt,my_sex_txt,my_type_txt;
        //为了触发列表事件
        LinearLayout mainLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //获得my_row布局文件里的控件
            my_id_txt = itemView.findViewById(R.id.my_id_text);
            my_age_txt = itemView.findViewById(R.id.my_age_text);
            my_height_txt = itemView.findViewById(R.id.my_height_text);
            my_weight_txt = itemView.findViewById(R.id.my_weight_text);
            my_sex_txt = itemView.findViewById(R.id.my_sex_text);
            my_type_txt = itemView.findViewById(R.id.my_type_text);
//为了触发列表事件
            mainLayout = itemView.findViewById(R.id.mainLayout);
        }
    }




}
